import java.awt.Graphics2D;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.PlayField;


public abstract class StateGameObject extends GameObject {

	private StateManager myManager;
	protected PlayField currentPlayField;
	
	
	public StateGameObject(GameEngine e) {
		super(e);
		myManager = new StateManager();
		currentPlayField = new PlayField();
	}

	
	/**
	 * To use the StateManager, users must add their stuff to currentPlayField
	 */
	@Override
	public abstract void initResources();
	
	@Override
	public void render(Graphics2D g) {
		currentPlayField.render(g);
	}

	public StateManager getStateManager(){
		return myManager;
	}
	
	@Override
	public void update(long elapsedTime) {

		updateGame(elapsedTime);
		currentPlayField.update(elapsedTime);
		// stop updating from state table when there's nothing left to update from the state table	

		myManager.setPlayField(currentPlayField);
		myManager.updateTable(elapsedTime);
	}
	
	
	/**
	 * User uses updateGame as if you'd use update in GameObject subclass. Called in inherited update.
	 * @param elapsedTime
	 */
	public abstract void updateGame(long elapsedTime);
	
	public void replay(){
		Replay replay = new Replay(this.parent);
		replay.setTable(myManager.getStateTable());
		parent.nextGame = replay;
		finish();
	}
	public void updateFromState(State state){
		currentPlayField = state.getPlayField();
	}

}
